#include <stdio.h>
#include <iostream>
#include <time.h>
#include "ParameterLearning.h"
#include "ParameterLearningFactory.h"
#include "Environment.h"
#include "DataSet.h"
#include "AllFeatureClasses.h"
#include "Feature.h"
#include "FeatureFactory.h"
#include "ExtendedPGMStruct.h"

#define	defaultConfigFile	"./PGMConfigFile.conf"

int main(int argc, char *argv[]) {
	// Setting parameters of random function 'rand'
	srand ( (unsigned int) time(NULL) );
	// Reading configuration parameters
	Environment environment;
	environment.readConfigFile(defaultConfigFile);
	
	// Creating object for data set and reading info from file (whose address is in environment)
	std::string dataSetName, dataSetPath;
	if (environment.getStringParamValue(dataSetPath, "DataSetDir") != 0) {
		std::cout << "Error in main file: cannot find 'DataSetDir' in config file.\n";
		return -1;
	}
	if (environment.getStringParamValue(dataSetName, "DataSetMain") != 0) {
		std::cout << "Error in main file: cannot find 'DataSetMain' in config file.\n";
		return -1;
	}
	DataSet dataSet;
	dataSet.readDataSet(dataSetPath + "/" + dataSetName);
	// updating maximum values of the dataSet
	unsigned int maxVal = 0;
	for (unsigned int i = 0; i < dataSet.maxValues.size(); i++)
		if (dataSet.maxValues[i] > maxVal)
			maxVal = dataSet.maxValues[i];
	for (unsigned int i = 0; i < dataSet.maxValues.size(); i++)
		dataSet.maxValues[i] = maxVal;


	// TEMPORARY INITIALIZATION OF Y VALUES IN THE FIRST TIMESTAMP IN THE DATASET
	if (dataSet.data.size() < 1) {
		std::cout << "Error in main file: data set is empty.\n";
		return -1;
	}
	/* // Setting randomly values of Yij
	for (unsigned int i = 0; i < (dataSet.data[0].size() / 2); i++) {
		if (dataSet.data[0][i] == 1) {
			if (((double) rand() / (double) RAND_MAX) < 0.3) // this defines how many Yij = 1 and how many Yij = 2
				dataSet.data[0][i] = 2;
		}
		dataSet.data[1][i] = std::max( dataSet.data[0][i], dataSet.data[1][i] );
		if (dataSet.data[1][i] == 1) {
			dataSet.data[1][i] = 0;
		}
	}
	*/


	// Second data set - describing attributes
	if (environment.getStringParamValue(dataSetName, "DataSetAttr") != 0) {
		std::cout << "Error in main file: cannot find 'DataSetAttr' in config file.\n";
		return -1;
	}
	DataSet dataSetAttr;
	dataSetAttr.readDataSet(dataSetPath + "/" + dataSetName);

	// Creating a PGM and initializing it according to data set
	ExtendedPGMStruct extPGMStruct;
	extPGMStruct.initStructure (environment);
	extPGMStruct.initStructure (dataSet, dataSetAttr);

	// Running learning part
	extPGMStruct.learn(dataSet);
	
	// Debug thing - to see the console before closing
	std::cout << "\nPress enter to close.";
	std::cin.get();

    return 0;
}
